﻿using System;
using System.Linq;
using WebExam.DataEntities;
using WebExam.DataProvider;

namespace WebExam.SQLDataProvider
{
    public class ZYRepository : BaseRepository<ZY>, IZYRepository
    {
        /// <summary>
        /// 获取专业信息
        /// </summary>
        /// <param name="start">开始位置</param>
        /// <param name="takeSize">获取条数</param>
        public IQueryable<ZY> GetAll(int start, int takeSize)
        {
            using (ThisDb)
            {
                return ThisTable.OrderBy(p => p.YXNum).Skip(start).Take(takeSize);
            }
        }

        /// <summary>
        /// 获取指定院系的专业信息
        /// </summary>
        /// <param name="yxId">院系Id</param>
        /// <param name="start">开始位置</param>
        /// <param name="takeSize">获取条数</param>
        public IQueryable<ZY> GetAll(int yxId, int start, int takeSize)
        {
            using (ThisDb)
            {
                return ThisTable.Where(p => p.YXNum == yxId).OrderBy(p => p.YXNum).Skip(start).Take(takeSize);
            }
        }

        /// <summary>
        /// 获取专业信息
        /// </summary>
        /// <param name="zyId">专业Id</param>
        public ZY Get(int zyId)
        {
            using (ThisDb)
            {
                return ThisTable.SingleOrDefault(p => p.ZYNum == zyId);
            }
        }

        /// <summary>
        /// 添加专业信息
        /// </summary>
        /// <param name="zy">专业对象</param>
        public ZY Add(ZY zy)
        {
            using (ThisDb)
            {
                try
                {
                    ThisTable.InsertOnSubmit(zy);
                    ThisDb.SubmitChanges();
                    return zy;
                }
                catch
                {
                    return zy;
                }
            }
        }

        /// <summary>
        /// 更新专业信息
        /// </summary>
        /// <param name="zyId">专业Id</param>
        /// <param name="update">包含更新操作的方法</param>
        public ZY Update(int zyId, Action<ZY> update)
        {
            using (ThisDb)
            {
                var zy = ThisTable.SingleOrDefault(p => p.ZYNum == zyId);

                if (zy == null)
                {
                    return null;
                }

                update(zy);

                try
                {
                    ThisDb.SubmitChanges();
                    return zy;
                }
                catch
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 删除专业信息
        /// </summary>
        /// <param name="zyId">专业Id</param>
        public bool Delete(int zyId)
        {
            using (ThisDb)
            {
                var zy = ThisTable.SingleOrDefault(p => p.ZYNum == zyId);

                if (zy == null)
                {
                    return true;
                }

                try
                {
                    ThisTable.DeleteOnSubmit(zy);
                    ThisDb.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 专业是否存在
        /// </summary>
        /// <param name="zyName">专业名称</param>
        public bool IsExists(string zyName)
        {
            using (ThisDb)
            {
                return ThisTable.Count(p => string.Equals(p.ZYName, zyName, StringComparison.Ordinal)) > 0;
            }
        }

        /// <summary>
        /// 获取专业个数 
        /// </summary>
        public int Count()
        {
            using (ThisDb)
            {
                return ThisTable.Count();
            }
        }

        /// <summary>
        /// 获取指定院系下的专业个数
        /// </summary>
        /// <param name="yxId">院系Id</param>
        public int Count(int yxId)
        {
            using (ThisDb)
            {
                return ThisTable.Count(p => p.YXNum == yxId);
            }
        }
    }
}